<template>
  <div>
    <ChangePwd ref="pwdModal"></ChangePwd>
    <a-dropdown v-if="currentUser && currentUser.name" placement="bottomRight">
      <span class="ant-pro-account-avatar">
        <a-avatar size="small" :src="currentUser.avatar" class="antd-pro-global-header-index-avatar" />
        <span>{{ currentUser.name }}</span>
      </span>
      <template v-slot:overlay>
        <a-menu class="ant-pro-drop-down menu" :selected-keys="[]">
          <a-menu-item key="center" @click="handleToCenter" v-if="false">
            <a-icon type="user" />
            个人中心
          </a-menu-item>
          <a-menu-item key="settings" @click="handleToSettings">
            <a-icon type="setting" />
            个人设置
          </a-menu-item>
          <a-menu-item key="pwd" @click="toChangPwd">
            <a-icon type="lock" />
            修改密码
          </a-menu-item>
          <a-menu-divider/>
          <a-menu-item key="logout" @click="handleLogout">
            <a-icon type="logout" />
            退出登录
          </a-menu-item>
        </a-menu>
      </template>
    </a-dropdown>
    <span v-else>
      <a-spin size="small" :style="{ marginLeft: 8, marginRight: 8 }" />
    </span>
  </div>
</template>

<script>
import { Modal } from 'ant-design-vue'
import ChangePwd from '@/views/account/personal/ChangePwd'
export default {
  components: {
    ChangePwd
  },
  methods: {
    handleToCenter () {
      this.$router.push({ path: '/account/center' })
    },
    handleToSettings () {
      this.$router.push({ path: '/account/settings' })
    },
    toChangPwd () {
      this.$refs.pwdModal.open()
    },
    handleLogout (e) {
      Modal.confirm({
        title: '信息',
        content: '您确定要注销吗？',
        onOk: () => {
          return this.$store.dispatch('logout').then(() => {
            this.$router.push({ name: 'login' })
          })
        },
        onCancel () {}
      })
    }
  }
}
</script>

<style lang="less" scoped>
.ant-pro-drop-down {
  :deep(.action) {
    margin-right: 8px;
  }
  :deep(.ant-dropdown-menu-item) {
    min-width: 160px;
  }
}
</style>
